/**
 * @file 工单操作-取消
 * @author yangjiangming <449654337@qq.com>
 * @date 2018-12-10 20:13:16
 */
layui.define(['layer', 'form', 'list_modify_common', '$http'], function (exports) {
    var $ = layui.$;
    let form = layui.form;
    var opCommonUtils = layui.list_modify_common;
    var layer = layui.layer;
    var $http = layui.$http;

    function getTagShortName(tagId) {
        for (var i = 0; i < tagList.length; i++) {
            var element = tagList[i];
            if (element.value.toString() === tagId) {
                return element.ext1;
            }
        }
    }

    function getTagName(tagId) {
        for (var i = 0; i < tagList.length; i++) {
            var element = tagList[i];
            if (element.value.toString() === tagId) {
                return element.text;
            }
        }
    }

    /**
     * 初始化页面
     */
    function initPage() {
    }


    /**
     * 初始化页面事件
     */
    function initEvent() {

        form.on('submit(save)', function (data) {
            console.log(data);

            var url = '/work/modify/save/ordertag.action';

            let field = data.field;

            let params = {
                orderId: field.orderId,
                workId: field.workId,
                remark: field.remark
            };

            let tags = [];

            var formData = $(data.form).serializeArray();
            for (var i = 0; i < formData.length; i++) {
                var formDatum = formData[i];
                if (formDatum.name === 'tag') {
                    var orderTag = {
                        tagId: formDatum.value,
                        tagName: getTagName(formDatum.value)
                    };
                    tags.push(orderTag);
                }
            }
            params.tags = tags;

            console.log(params);

            $http.lockSave(url, params, data.elem).then(function () {
                layer.msg('操作成功', {icon: 1, time: 1000}, function () {
                    opCommonUtils.goOperateHome();
                });
            }, function (msg) {
                layer.msg(msg);
            });

            return false;
        });


    }

    // 程序入口
    function init() {

        // 初始化页面
        initPage();

        // 初始化事件
        initEvent();

    }


    // 初始化
    init();

    exports('oms_work_modify_ordertag');
});
